package Zhang7;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

//class Book {
//    private String title;
//    //图书名称
//    private double price;
//
//    //图书价格
//    public Book(String title, double price) {
//        this.title = title;
////属性赋值
//        this.price = price;
//    }
//
//    @Override
//    public String toString() {
//        return "【图书】名称=" + this.title + "、价格=" + this.price;
//    }
//}
public class AtomicStampedReferenceZhang7 {
    public static void main(String[] args) throws Exception {
        Book book = new Book("Java就业编程实战",69.8);
        //实例化Book类对象
//由于AtomicStampedReference需要提供有版本号，所以在初始化时定义版本编号为1
       AtomicStampedReference<Book> ref = new AtomicStampedReference<Book>(book, 1);
//在进行CAS操作时除了要设置替换内容之外，也需要设置正确的版本号，否则无法替换
        ref.compareAndSet(book,new Book("Spring就业编程实战",67.8),1, 2);//CAS操作
        System.out.println(ref.getReference());
        System.out.println("当前版本号："+ref.getStamp());
    }
}
